﻿;(function(){
	$.fn.uploadFile=function(setting){
		var defaultOption={
			serviceCategory:'local',
			multi_selection:false,
			prevent_duplicates:false,
			autoStart:true,
			btnId:null,
			FilesAdded:null,
			BeforeUpload:null,
			UploadProgress:null,
			FileUploaded:null,
			fillId:null,
			fillName:null,
			fillImg:null
		};
		
		defaultOption=$.extend(defaultOption,setting);
		
		if($.isNull(defaultOption.fillId)){
			var id=$(this).attr("id");
			if(!$.isNull(id)){
				defaultOption.fillId="#"+id;	
			}
		}
		
		if($.isNull(defaultOption.btnId)){
			defaultOption.btnId="btnAddFile_default";	
		}
		console.log(defaultOption.btnId);
		if($("#"+defaultOption.btnId).length>0){
			$("#"+defaultOption.btnId).click();
			return;
		}
		
		var buttonHtml="<button type='button' id='"+defaultOption.btnId+"' style='display:none;'>上传</button>";
		$("body").append(buttonHtml);
		
		var pluploader= new plupload.Uploader({
			browse_button : defaultOption.btnId, 
			url:'',
			multi_selection:defaultOption.multi_selection,
			filters:{
				prevent_duplicates : defaultOption.prevent_duplicates
			},
			init: {
				PostInit:function(){
				},
				FilesAdded: function(up, files) {
					if(!$.isNull(defaultOption.FilesAdded)){
						defaultOption.FilesAdded(up, files);
					}
					if(defaultOption.autoStart){
						pluploader.start();	
					}
				},
				BeforeUpload: function(up, file) {
					var result=true;
					if(!$.isNull(defaultOption.BeforeUpload)){
						result=defaultOption.BeforeUpload(up, file);
					}
					//不是null，并且是false
					if(!$.isNull(result)&&!result){
						return;
					}
					if("local"===defaultOption.serviceCategory){
						return setLocalPluploadParam(up, file);
					}else if("oss"===defaultOption.serviceCategory){
						return setOssPluploadParam(up, file);	
					}
				},
				UploadProgress: function(up, file) {
					if(!$.isNull(defaultOption.UploadProgress)){
						defaultOption.UploadProgress(up, files);
					}
				},
				FileUploaded: function(up, file, info) {
					if(info.status==200){
						var responseJson=JSON.parse(info.response);
						if(responseJson.status=="ok"){
							if(!$.isNull(defaultOption.fillId)){
								$(defaultOption.fillId).val(responseJson.id);
							}
							if(!$.isNull(defaultOption.fillName)){
								$(defaultOption.fillName).val(file.name);
							}
							if(!$.isNull(defaultOption.fillImg)){
								$(defaultOption.fillImg).attr("src",$.getFileUrl(responseJson.id));
							}
						}
					}
					if(!$.isNull(defaultOption.FileUploaded)){
						defaultOption.FileUploaded(up, file, info);
					}
				}
			}
		});
		pluploader.init();
		
		setTimeout(function(){
			$("#"+defaultOption.btnId).click();	
		},100);
		
		return pluploader;
	}
})(jQuery);

function setOssPluploadParam(up, file){
	var filename=file.name;
	var url= $.getVirtualPath()+"/file/ossfile/ossSignature";
	var uploadConfig=$.getJsonPost(url);
	new_multipart_params={
        'key' : uploadConfig.dir+uploadConfig.uuid+filename.substr(filename.lastIndexOf("."),filename.length),
		'policy': uploadConfig.policy,
        'OSSAccessKeyId': uploadConfig.accessid, 
        'success_action_status' : '200', //让服务端返回200,不然，默认会返回204
		'signature': uploadConfig.signature,
		'callback':uploadConfig.callback,
		'x:displayname':encodeURI(filename)
	}
	$.getSha1(file.getNative(),function(){
		//设置文件的sha1值
		new_multipart_params['x:sha1']=sha1;
		up.setOption({'url':uploadConfig.host,'multipart_params': new_multipart_params});	
		
		file.status = plupload.UPLOADING;
        up.trigger("UploadFile", file);
	});
	
	return false;
}

function setLocalPluploadParam(up,file){
	var filename=file.name;
	var url= $.getVirtualPath()+"/file/localFile/localInfo";
	var uploadConfig=$.getJsonPost(url);
	
	up.setOption({'url':uploadConfig.host});
	
	return true;
}